package com.hfzy.ihk.web.calldata.biz;

import com.hfzy.ihk.common.constants.GlobalStatic;
import com.hfzy.ihk.common.core.elasticsearch.service.EsBaseServiceImpl;
import com.hfzy.ihk.common.web.WebResult;
import com.hfzy.ihk.facade.calldata.entity.CallHistory;
import com.hfzy.ihk.facade.calldata.enums.CallDataRedisVersionKey;
import com.hfzy.ihk.facade.calldata.enums.CallDataTables;
import com.hfzy.ihk.web.calldata.dao.CallHistoryRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SourceFilter;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by Administrator on 2018/8/14.
 */
@Component
public class CallHistoryBiz extends EsBaseServiceImpl<CallHistory>{

    @Autowired
    CallHistoryRepository callHistoryRepository;

    @Override
    protected ElasticsearchCrudRepository getReposity() {
        return callHistoryRepository;
    }

    /**
     * 新增通话记录
     * @param callHistory
     * @return
     */
    public WebResult saveCallHistory(CallHistory callHistory){
        WebResult result = new WebResult();
        boolean flag = createDataAndSetVersion(CallDataRedisVersionKey.TABLE_CALL_HISTORY,
                CallDataTables.CALL_HISTORY,
                callHistory.getSessionId(),
                callHistory.getVersion(),
                ()-> callHistoryRepository.save(callHistory));

        if(!flag){
            result.setCode(GlobalStatic.CODE_N);
            result.setMsg("保存出错");
            return result;
        }

        result.setCode(GlobalStatic.CODE_1);
        result.setMsg("成功");

        return result;
    }

    /**
     * 修改通话记录
     * @param updateMap
     * @param callHistoryId
     * @param version
     * @return
     */
    public WebResult updateCallHistory(Map<String,Object> updateMap,String callHistoryId,Long version){
        WebResult result = new WebResult();
        updateMap.put("version",version + 1);
        boolean flag = checkVersionAndUpdateData(CallDataRedisVersionKey.TABLE_CALL_HISTORY,
                CallDataTables.CALL_HISTORY,
                callHistoryId,
                version,
                ()-> callHistoryRepository.updateSomeFields(callHistoryId,CallHistory.class,updateMap));

        if(!flag){
            result.setCode(GlobalStatic.CODE_N);
            result.setMsg("保存出错");
            return result;
        }

        result.setCode(GlobalStatic.CODE_1);
        result.setMsg("成功");

        return result;
    }

    /**
     * 分页查询通话记录
     * @param boolQueryBuilder
     * @param sort
     * @param pageable
     * @return
     * @throws Exception
     */
    public Page<CallHistory> queryCallHistoryPageList(BoolQueryBuilder boolQueryBuilder, FieldSortBuilder sort, Pageable pageable, SourceFilter sourceFilter)throws Exception{
        if(pageable == null){
            pageable = PageRequest.of(0,10);
        }
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder().withPageable(pageable);
        if(boolQueryBuilder !=null && boolQueryBuilder.hasClauses()){
            builder.withQuery(boolQueryBuilder);
        }
        if(sort != null){
            builder.withSort(sort);
        }
        if(sourceFilter!=null){
            builder.withSourceFilter(sourceFilter);
        }
        Page<CallHistory> page = callHistoryRepository.search(builder.build());
        return page;
    }
}
